##
## This file is part of the django-oidauth project.
##
## Copyright (C) 2009 Filippo Santovito <http://mementomori.myvidoop.com/>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License version 3 as
## published by the Free Software Foundation;
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
##

from datetime import datetime
from django.conf import settings

__all__ = ['get_trust_root', 'get_store_path', 'create_user_profile']

def get_trust_root():
    return getattr(settings, 'OPENID_TRUST_ROOT')

def get_store_path():
    return getattr(settings, 'OPENID_STORE_PATH')

def create_user_profile(user, openid_url, profile_data):

    try:
        profile_data = dict(profile_data)
    except TypeError, e:
        profile_data = {}
        
    """
    a dob can like '1980-00-00'.
    I'll consider it an invalid date
    """
    if 'dob' in profile_data:
        try: 
            datetime(*[int(x) for x in profile_data['dob'].split('-')]) 
        except ValueError:  
            profile_data['dob'] = None

    #email will be saved in the User model
    if 'email' in profile_data:
        user.email = profile_data['email']
        del profile_data['email']

        
    profile_data['url'] = openid_url
    user.openid_set.create(**profile_data)
    user.get_profile().save()
    user.save()
    
